<template>
  <div class="detail-container">
    <el-form
      ref="postForm"
      :model="postForm"
      :rules="rules"
      label-width="100px"
      label-suffix=":"
    >

      <sticky :z-index="10" :class-name="'sub-navbar ' + postForm.color">
        <el-button v-loading="loading" type="success" @click="submitForm">
          {{ isEdit ? '更新' : '保存' }}
        </el-button>
      </sticky>

      <div class="app-container" style="padding: 10px 20px">
        <el-row>
          <el-col :span="22">
            <el-form-item prop="image" label="店铺照片">
              <upload-image
                bucket="supplier"
                :max="5"
                :width="750"
                :height="750"
                :quality="50"
                :file-list="postForm.image"
                @onSuccess="onSuccess"
                @onRemove="onRemove"
              />
              <span>最多可以上传5张, 建议尺寸 350 * 350 px</span>
            </el-form-item>
            <el-form-item label="简介" prop="desc" required>
              <el-input type="textarea" v-model.trim="postForm.desc" :rows="10" />
            </el-form-item>
          </el-col>
        </el-row>
      </div>

    </el-form>
  </div>
</template>

<script>
import api from '@/api'
import Sticky from '@/components/Sticky'
import UploadImage from '@/components/Upload/UploadImage'
export default {
  name: 'ShopDesc',
  components: {
    Sticky,
    UploadImage
  },
  data() {
    const descValidator = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入简介'))
      } else if (value.length < 10) {
        callback(new Error('长度不能小于10位'))
      } else {
        callback()
      }
    }
    const imageValidator = (rule, value, callback) => {
      if (this.postForm.image.length === 0) {
        callback(new Error('请上传店铺照片'))
      } else {
        callback()
      }
    }
    return {
      id: -1,
      loading: false,
      postForm: {
        color: 'draft',
        status: true,
        sort: 0,
        image: []
      },
      rules: {
        desc: [{ validator: descValidator, trigger: 'blur' }],
        image: [{ validator: imageValidator, trigger: 'change' }]
      },
      // 复制过来的, 直接调用数据
      isEdit: true
    }
  },
  created() {
    /**
     * 获取编辑数据
     */
    if (this.isEdit) {
      this.id = (this.$route.params && this.$route.params.id) || -1
      this.fetchData(this.id)
    }
  },
  methods: {
    /**
     * 获取编辑数据
     */
    async fetchData() {
      const response = await api.supplier.view()
      this.postForm = Object.assign({}, response.data.model)
      // 图片
      if (this.postForm.descImage) {
        const image = this.postForm.descImage.map(x => x.image)
        const list = []
        image.forEach(x => {
          if (x) {
            list.push({
              id: +x.id,
              url: x.thumbnail_path
            })
          }
        })
        this.postForm.image = [...list]
      } else {
        this.postForm.image = []
      }
    },
    /**
     * 提交表单
     */
    submitForm() {
      this.$refs.postForm.validate(valid => {
        if (valid) {
          // 图片
          let image = []
          if (this.postForm.image.length > 0) {
            image = this.postForm.image.map(x => x.id)
          }
          api.supplier.edit({
            ...this.postForm,
            image
          }).then(response => {
            this.loading = false
            this.$notify({
              title: '成功',
              message: response.message || '操作成功',
              type: 'success',
              duration: 2000
            })
          }).finally(() => {
            this.loading = false
          })
        }
      })
    },
    /**
     * 图片上传
     */
    onSuccess(data, fileList) {
      if (data && data.id) {
        this.postForm.image.push({
          id: data.id,
          url: data.thumbnail
        })
      }
    },
    /**
     * 删除图片
     */
    onRemove(data, fileList) {
      if (data && data.id) {
        this.postForm.image = this.postForm.image.filter(x => x.id !== data.id)
      }
    }
  }
}
</script>

<style scoped lang="scss">

</style>
